package com.groupscore.score.dao;

import com.groupscore.score.pojo.Lotter;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

public interface LotterDao extends JpaRepository<Lotter,String>, JpaSpecificationExecutor<Lotter> {

    /**
     * 随机小组
     * @return
     */
    @Query(value = "select id,group_id from user GROUP BY group_id order by rand() limit 4 ",nativeQuery = true)
    List<Map> randGroup();

    /**
     * 根据小组id,随机查询一个小组成员
     */
    @Query(value = "select id as userId,group_id as groupId,nikename as userName from user where group_id = ? order by rand() limit 1 ",nativeQuery = true)
    Map randGroupMember(String groupId);

    /**
     * 随机个人
     */
    @Query(value = "SELECT id,nikename from user_draw where flag = '0' GROUP BY id order by rand() limit 1", nativeQuery = true)
    public Map randMember();

    /**
     * 确定
     */
    @Transactional
    @Modifying
    @Query(value = "update user_draw set flag = '1' where id = ?1", nativeQuery = true)
    public void updateMember(String id);
}
